#include <bits/stdc++.h>
using namespace std;

map<string, set<int>> title, author, key, publisher, year;
int main()
{
	int n, i, tid;
	cin >> n;
	string tstr;
	for(i = 0; i < n; i++)
	{
		cin >> tid;
		getline(cin, tstr);
		getline(cin, tstr);
//		cout << "test:" << tstr << endl;
		title[tstr].insert(tid);
		getline(cin, tstr);
//		cout << "test:" << tstr << endl;
		author[tstr].insert(tid);
		while(true)
		{
			cin >> tstr;
//			cout << "test:" << tstr << endl;
			key[tstr].insert(tid);
			if(getchar() == '\n')
				break; 
		}
		getline(cin, tstr);
//		cout << "test:" << tstr << endl;
		publisher[tstr].insert(tid);
		getline(cin, tstr);
//		cout << "test:" << tstr << endl;
		year[tstr].insert(tid);
	}
	cin >> n;
	for(i = 0;i < n; i++)
	{
		scanf("%d: ", &tid);
		getline(cin, tstr);
		set<int> temp;
		if(tid == 1) temp = title[tstr];
		if(tid == 2) temp = author[tstr];
		if(tid == 3) temp = key[tstr];
		if(tid == 4) temp = publisher[tstr];
		if(tid == 5) temp = year[tstr];
		cout << tid << ": " << tstr << endl;
		if(temp.begin() == temp.end())
		{
			cout << "Not Found" << endl;
			continue;
		}
		for(set<int>::iterator it = temp.begin(); it != temp.end(); it++)
			printf("%07d\n", *it);
	}
	return 0;
}
